package cn.llssit.controller;

import cn.llssit.domain.Movie;
import cn.llssit.framework.web.controller.BaseController;
import cn.llssit.framework.web.domain.AjaxResult;
import cn.llssit.mapper.MovieMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: Lesan
 * @Date: 2020-06-21
 */
@RestController
@RequestMapping("/movie")
public class MovieController extends BaseController {

    @Autowired
    private MovieMapper movieMapper;

    @ApiOperation("")
    @GetMapping("/list")
    public AjaxResult getlist(Integer pageNum, Integer pageSize,String status,String name){
        if (status==null&&name==null){
            PageHelper.startPage(pageNum,pageSize);
            Page<Movie> movies = (Page<Movie>) movieMapper.selectAll();
            PageInfo<Movie> pageInfo = new PageInfo<>(movies);
            return AjaxResult.success(pageInfo);
        }
        if (status!=null){
            PageHelper.startPage(pageNum,pageSize);
            Page<Movie> movies = (Page<Movie>) movieMapper.selectByState(status);
            PageInfo<Movie> pageInfo = new PageInfo<>(movies);
            return AjaxResult.success(pageInfo);
        }
        if (name!=null){
            PageHelper.startPage(pageNum,pageSize);
            Page<Movie> movies = (Page<Movie>) movieMapper.selectByName(name);
            PageInfo<Movie> pageInfo = new PageInfo<>(movies);
            return AjaxResult.success(pageInfo);
        }
        return AjaxResult.error();
    }

    @ApiOperation("")
    @GetMapping("/getMovieById/{id}")
    public AjaxResult getMovieById(@Validated @PathVariable Integer id){
        return AjaxResult.success(movieMapper.selectById(id));
    }

    @ApiOperation("")
    @GetMapping("/updateShowState")
    public AjaxResult updateShowState(Integer id,Integer show){
        movieMapper.updateShowState(show,id);
        return AjaxResult.success();
    }

    @ApiOperation("")
    @PostMapping("/addMovie")
    public AjaxResult addMovie(@Validated @RequestBody Movie movie){
        movieMapper.addMovie(movie);
        return AjaxResult.success();
    }

    @ApiOperation("")
    @PostMapping("/updateMovie")
    public AjaxResult updateMovie(@Validated @RequestBody Movie movie){
        movieMapper.updateMovie(movie);
        return AjaxResult.success();
    }

    @ApiOperation("")
    @DeleteMapping("/deleteMovie/{ids}")
    public AjaxResult deleteMovie(@PathVariable Integer[] ids){
        for (Integer id : ids){
            movieMapper.deleteMovie(id);
        }
        return AjaxResult.success();
    }

}
